package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: rongfeng
 * @date: 2020/12/10
 * @description
 */
public class DowdLoadImg2 {
    static List<DataList> urlList = new ArrayList<>();

    public static class DataList implements Serializable {
        public String sourceUrl;
        public int index;

        public DataList(String sourceUrl) {
            this.sourceUrl = sourceUrl;
        }

    }


    public void addList(List<DataList> list) {
        int i = 0;
        for (DataList item : list) {
            item.index = i;
            urlList.add(item);
            i++;
        }
        new Down().start();
    }


    static class Down {
        volatile int mIndex = -1;
        ExecutorService pool;

        public void start() {
            pool = Executors.newFixedThreadPool(3);
            for (int i = 0; i < 3; i++) {
                next();
            }
        }

        public void next() {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    mIndex++;
                    if (mIndex < urlList.size()) {
                        download(urlList.get(mIndex));
                    }
                }
            });
        }

        public void download(DataList data) {
            // 下载网络文件
            int bytesum = 0;
            int byteread = 0;

            URL url = null;
            try {
                url = new URL(data.sourceUrl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            InputStream inStream = null;
            FileOutputStream fs = null;
            try {
                URLConnection conn = url.openConnection();
                inStream = conn.getInputStream();

                fs = new FileOutputStream("D:\\wrf\\qn\\" + data.index + ".jpg");

                byte[] buffer = new byte[1024];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    fs.write(buffer, 0, byteread);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inStream != null) {
                    try {
                        inStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fs != null) {
                    try {
                        fs.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            next();
            System.out.println("ssss " + mIndex);
        }


    }

}
